﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class BAKAB45AV95 : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public INTEXDataSet setUI;
        public INTEXDataSet setWorker = new INTEXDataSet();
        BAKAB45AV95Data m_Data, m_DataX;
        BAKAB45AV95Report m_rpt;
        public DataSet setResult;
        
        private DataRow dwUIABPOS;
        ExcelX.ExcelLib.ExcelAgent m_exAgent;
        DataSet setANINFOR = new DataSet();
        public BAKAB45AV95()
        {
            InitializeComponent();
            this.Name = "浆纱生产状态报表";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
           

            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            setUI = (INTEXDataSet)TryFindResource("iNTEXDataSet");
            m_Data = new BAKAB45AV95Data(OracleDB,proxy); 
            string filePath = @"C:\intex.w\Report\Planning\浆纱\SBAB45INFO.xls";
            if (System.IO.File.Exists(filePath))
            {
                m_exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                m_exAgent.iStart = 2;
                int CurrentMaxColumns = 0;
                m_exAgent.loadDataTableColumns("", ref CurrentMaxColumns, 100);
                m_exAgent.ReadRangeValue2DataTable(3, 3, 1, CurrentMaxColumns);
                filePath = filePath.Replace("SBAB45INFO.xls", "SBAB45INFO.XML");
                m_exAgent.DataSource.DataSetName = "BAB45INFO";
                m_exAgent.DataSource.Tables[0].TableName = "ABPOS";
                m_exAgent.DataSource.WriteXml(filePath);
                setANINFOR.Clear();
                setANINFOR.Merge(m_exAgent.DataSource.Tables[0]);
                //SndDg.DataContext = setANINFOR.Tables[0];
            }/*
            if (System.IO.File.Exists(filePath))
            {
                return;
                setANINFOR.ReadXml(filePath);
                setANINFOR.Tables[0].TableName = "ABPOS";
                SndDg.DataContext = setANINFOR.Tables[0];
            }*/
        }
        #endregion
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                finishedcount = 0;
                setUI.Clear();
                zoneQueryCond.IsExpanded = false;
                setUI.AcceptChanges();

                dwUIABPOS["ABPAUFART"] = "VW";
                string KRS = txtKRS.Text.Trim();
                string LFD = txtLFD.Text.Trim();
                string POS = txtPOS.Text.Trim();
                string ERLKZ = txtERLKZ.IsChecked.Value.ToString();
                if (ERLKZ == "True")
                {
                    dwUIABPOS["ABPERLKZ"] = "J";
                }
                else
                {
                    dwUIABPOS["ABPERLKZ"] = " ";
                }

                //KRS = "SB";
                //LFD = "000483";
                //POS = "010";
                dwUIABPOS["ABPAUFKRS"] = KRS;
                dwUIABPOS["ABPAUFLFD"] = "";
                if (LFD.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFLFD"] = m_Data.Right("000000" + LFD, 6);
                }
                dwUIABPOS["ABPAUFPOS"] = "";
                if (POS.Trim().Length > 0)
                {
                    dwUIABPOS["ABPAUFPOS"] = m_Data.Right("000" + POS, 3);
                }

                dwUIABPOS["ABPZUGET"] = "0";
                dwUIABPOS["ABPART"] = txtArticle.Text.ToString().Trim();
                string FACTORY = txtFACTORY.Text.ToString().Trim();
                if (FACTORY.Trim().Length > 0)
                {
                    dwUIABPOS["LSNAME1"] = m_Data.Right("000000" + FACTORY, 8);
                }
                setResult = m_Data.LoadABPOS(dwUIABPOS);
                setUI.Clear();
                setUI.Merge(setResult);
                /*foreach (DataTable dtTemp in setResult.Tables)
                {
                    setUI = (INTEXDataSet)m_Data.ImportDataTable(setUI, dtTemp, true); 
                }
                */
                totalrecord = setUI.ABPOS.Rows.Count;
                foreach (DataRow dwNABPOS in setUI.ABPOS.Rows)
                { 
                    ThreadCount++;
                    EventDispatcher.Instance.RaiseUIMessage("BAKAB45AV95Data", dwNABPOS, OracleDB, proxy,setUI);
                }
            }
            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUIABPOS = setUI.ABPOS.NewRow();
                if (setUI != null)
                {
                    setResult = m_Data.GetWASTAMM();
                    setUI.Merge(setResult);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string PrintType = "JustExcel";
                GenerateReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            string PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        public void GenerateReport(string PrintType)
        {
            Process[] AllProcesses = Process.GetProcessesByName("EXCEL");

            // check to kill the right process
            /*foreach (Process ExcelProcess in AllProcesses)
            {
                ExcelProcess.Kill();
            }*/
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                if (setANINFOR.Tables.Contains("ABPOS"))
                {
                    setANINFOR.Tables["ABPOS"].Clear();
                }
                string VWORDERNUM = "";
                DataRow dwABINFO = null;
                bool flgNew = false;
                foreach (DataRow dwTemp in setUI.Tables[0].Rows)
                {
                    VWORDERNUM = dwTemp["VWORDERNUM"].ToString();
                    var query = from DataRow dwExist in setANINFOR.Tables[0].AsEnumerable()
                                where dwExist["VWORDERNUM"].ToString() == VWORDERNUM
                                select dwExist;
                    if (query.Count() > 0)
                    {
                        foreach (DataRow dwExist in query)
                        {
                            dwABINFO = dwExist;
                        }
                    }
                    else
                    {
                        dwABINFO = setANINFOR.Tables["ABPOS"].NewRow();
                        flgNew = true;
                    }
                    foreach (DataColumn dcTemp in dwABINFO.Table.Columns)
                    {
                        string column = dcTemp.ColumnName;
                        if (dwTemp.Table.Columns.Contains(column.Trim()))
                        {
                            dwABINFO[column] = dwTemp[column.Trim()];
                        }
                    }
                    if (flgNew)
                    {
                        flgNew = false;
                        setANINFOR.Tables[0].Rows.Add(dwABINFO);
                    }
                    //setANINFOR.Merge(setUI.Tables["ABPOS"]);
                }
            }
            if (setANINFOR.Tables.Contains("GAAUF"))
            {
                setANINFOR.Tables["GAAUF"].Clear();
            }
            if (setUI.Tables.Contains("GAAUF"))
            {
                setANINFOR.Merge(setUI.Tables["GAAUF"]);
            } 
            m_DataX = new BAKAB45AV95Data(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            m_rpt = new BAKAB45AV95Report(dicParamater);
            m_rpt.dsSource = setANINFOR;
            m_rpt.PrintType = PrintType;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }
        public void ItemsControls_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {

                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
